package CoteClient;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import CoteServeur.Langue;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;


public class VueInscription extends Vue{

	private JPanel contentPane;
	private JTextField textFieldNCompte;
	private JTextField textFieldOrg;
	private JPasswordField textFieldMdp;
	private JPasswordField textFieldMdpConf;
	private JComboBox comboBox;
	private JRadioButton rdbtnBase;
	private JRadioButton rdbtnMoyen;
	private JRadioButton rdbtnEleve;
	private ButtonGroup g = new ButtonGroup(); //Sert à regrouper les boutons entre et n'en laisser qu'un selectionnable.
	
	public VueInscription(Controleur controleur) {
		super(controleur);
		setTitle("Inscription");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 430, 300);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnErim = new JMenu("Menu");
		menuBar.add(mnErim);
		
		JMenuItem mntmQuitter = new JMenuItem("Quitter");
		mnErim.add(mntmQuitter);
		
		JMenu mnNewMenu = new JMenu("Indentification");
		menuBar.add(mnNewMenu);
		
		JMenuItem mntmLogin = new JMenuItem("Login");
		mntmLogin.setEnabled(false);
		mnNewMenu.add(mntmLogin);
		
		JMenuItem mntmLogout = new JMenuItem("Logout");
		mntmLogout.setEnabled(false);
		mnNewMenu.add(mntmLogout);
		
		JMenuItem mntmSignUp = new JMenuItem("Sign up");
		mnNewMenu.add(mntmSignUp);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblLabelNCompte = new JLabel("Nom de compte");
		lblLabelNCompte.setHorizontalAlignment(SwingConstants.RIGHT);
		lblLabelNCompte.setBounds(13, 12, 124, 15);
		contentPane.add(lblLabelNCompte);
		
		JLabel lblLabelOrg = new JLabel("Organisme");
		lblLabelOrg.setHorizontalAlignment(SwingConstants.RIGHT);
		lblLabelOrg.setBounds(60, 39, 77, 15);
		contentPane.add(lblLabelOrg);
		
		JLabel lblLangue = new JLabel("Langue");
		lblLangue.setHorizontalAlignment(SwingConstants.RIGHT);
		lblLangue.setBounds(76, 66, 61, 15);
		contentPane.add(lblLangue);
		
		JLabel lblMotDePasse = new JLabel("Mot de passe");
		lblMotDePasse.setHorizontalAlignment(SwingConstants.RIGHT);
		lblMotDePasse.setBounds(30, 154, 107, 15);
		contentPane.add(lblMotDePasse);
		
		JLabel lblConfirmation = new JLabel("Confirmation");
		lblConfirmation.setHorizontalAlignment(SwingConstants.RIGHT);
		lblConfirmation.setBounds(30, 186, 107, 15);
		contentPane.add(lblConfirmation);
		
		textFieldNCompte = new JTextField();
		textFieldNCompte.setBounds(172, 10, 208, 20);
		contentPane.add(textFieldNCompte);
		textFieldNCompte.setColumns(10);
		
		textFieldOrg = new JTextField();
		textFieldOrg.setBounds(172, 37, 208, 20);
		contentPane.add(textFieldOrg);
		textFieldOrg.setColumns(10);
		
		textFieldMdp = new JPasswordField();
		textFieldMdp.setToolTipText("Le mot de passe doit contenir 5 caractères minimum dont 1 chiffre.");
		textFieldMdp.setBounds(172, 152, 208, 20);
		contentPane.add(textFieldMdp);
		textFieldMdp.setColumns(10);
		
		textFieldMdpConf = new JPasswordField();
		textFieldMdpConf.setToolTipText("");
		textFieldMdpConf.setBounds(172, 184, 208, 20);
		contentPane.add(textFieldMdpConf);
		textFieldMdpConf.setColumns(10);
		
		
		
		
		
		JButton btnInscription = new JButton("Inscription");
		btnInscription.setBounds(172, 216, 102, 25);
		btnInscription.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent e) {
				Langue.Niveau niv;
				if (rdbtnBase.isSelected()) {
					niv = Langue.Niveau.values()[0];
					}
				else if (rdbtnMoyen.isSelected()) {
					niv = Langue.Niveau.values()[1];
					}
				else {
					niv = Langue.Niveau.values()[2];
					}
				
				getControleur().inscription(textFieldNCompte.getText(), textFieldOrg.getText(), 
						(Langue.Intitule)comboBox.getSelectedItem(),niv, textFieldMdp.getText(), 
						textFieldMdpConf.getText());
				}
			
		});
		contentPane.add(btnInscription);
		
		JButton btnRetour = new JButton("Retour");
		btnRetour.setBounds(278, 216, 102, 25);
		btnRetour.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getControleur().connecterUtilisateur();
				}
		});
		contentPane.add(btnRetour);
		
		comboBox = new JComboBox();
		alimenteLangue();
		comboBox.setBounds(172, 61, 208, 24);
		contentPane.add(comboBox);
		
		String langue = String.valueOf(comboBox.getSelectedItem());
		
		
		rdbtnBase = new JRadioButton("Basique");
		rdbtnBase.setSelected(true);
		rdbtnBase.setBounds(172, 105, 77, 23);
		contentPane.add(rdbtnBase);
		
		rdbtnMoyen = new JRadioButton("Moyen");
		rdbtnMoyen.setBounds(246, 105, 66, 23);
		contentPane.add(rdbtnMoyen);
		
		rdbtnEleve = new JRadioButton("Elevé");
		rdbtnEleve.setBounds(316, 105, 66, 23);
		contentPane.add(rdbtnEleve);
		
		
		//On lie les boutons radio pour ne pouvoir n'en sélectionner qu'un seul et on met basique par défaut.
	    g.add(rdbtnBase);
	    g.add(rdbtnMoyen);
	    g.add(rdbtnEleve);
	}
	
	public void alimenteLangue() {
	int intit = Langue.Intitule.values().length;
	for(int i = 0; i < intit; i++)
		comboBox.addItem(Langue.Intitule.values()[i]);		
	}
		
}